// 封装的展示消息提示的方法
uni.$zm_showMsg = (title = '数据加载中...', duration = 2000,mask = true,icon) => {
	return new Promise((resolve, reject)=>{
		uni.showToast({
			title,
			duration,
			icon: 'none',
			mask,
			success(res) { resolve(res) },
			fail(res) { reject(res) },
			complete(res) { resolve(res) }
		})
	})
}

// 封装modal提示框
uni.$zm_showModal = (title, content, confrimText = '', cancelText = '', showCancel = false,  confirmColor = '', cancelColor = '') => {
    return new Promise((resolve, reject) => {
		uni.showModal({
			title: title || '提示',
			content: content || '确定要执行操作吗？',
			confirmText: confrimText || '确定',
			cancelText: cancelText || '取消',
			showCancel: showCancel,
			cancelColor: cancelColor || '#000000',
			confirmColor: confirmColor || '#000000',
			success(res) { resolve(res) },
			fail(res) { reject(res) },
			complete(res) { resolve(res) }
		})
    })
}

/**
 * 显示加载进度提示
 * @param {boolean} mask 是否显示透明蒙层(默认显示true)
 */
uni.$zm_showLoading = (title, mask = true)=> {
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: title || '加载中',
			mask,
			success(res) { resolve(res) },
			fail(res) { reject(res) },
			complete(res) { resolve(res) }
		})
	})
}

// 隐藏加载进度提示
uni.$zm_hideLoading = ()=> {
	return new Promise((resolve, reject) => {
		uni.hideLoading({
			success: (res) => { resolve(res) },
			fail: (res) => { reject(res) },
			complete: (res) => { resolve(res) },
		})
	})
}

// 页面跳转
uni.$jumpPage = (url, options) => {
	if (options) {
		if (options.switchTab) {
			uni.switchTab({
				url
			})
		} else if (options.reLaunch) {
			uni.reLaunch({
				url
			});
		} else if (options.redirectTo) {
			uni.redirectTo({
				url
			});
		} else if (options.back) {
			// let routes = getCurrentPages().length;
			uni.navigateBack({
				delta: options.back
			});
		} else {
			uni.navigateTo({
				url
			})
		}
	} else {
		uni.navigateTo({
			url,
			fail() { uni.switchTab({url: url,}) }
		})
	}
}

// 页面跳转
uni.$jumpPage2 = (url, options)=> {
	if (options) {
		if (options.redirectTo) {
			uni.redirectTo({
				url,
				fail() { uni.switchTab({url: url,}) }
			});
		}
	} else {
		uni.navigateTo({
			url,
			fail() { uni.switchTab({url: url,}) }
		})
	}
}

uni.$zm_pageScrollTo = ( duration = 500,scrollTop = 0 ) => {		// 事件触发的滚动
	return new Promise((resolve, reject)=>{
		uni.pageScrollTo({
			duration,
		    scrollTop
		})
	})
}


/**
 * 版本比较是否有最新版本
 * 
 * @param {string} local_version 本机版本号
 * @param {string} api_version   接口版本号
 * @returns {boolean} 
 */
uni.$mj_compareVersion = (local_version, api_version) => {
	let v1 = local_version.split('.')
	let v2 = api_version.split('.')
	const len = Math.max(v1.length, v2.length)
	while (v1.length < len) {
		v1.push('0')
	}
	while (v2.length < len) {
		v2.push('0')
	}

	for (let i = 0; i < len; i++) {
		const num1 = parseInt(v1[i])
		const num2 = parseInt(v2[i])
		if (num1 > num2) {
			return false
		} else if (num1 < num2) {
			return true
		}
	}
	return false
}